<?php
namespace app\admin\controller;
use think\Db;
use lib\Curl;
class Banner extends Base{
    public function banner_list(){
        $list=Db::name('banner')->where('state','<',3)->order('id desc')->paginate(10);
        $lists=Db::name('banner')->where('state','<',3)->select();
        $data=$list->all();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function banner_add(){
        if(request()->isGet()){
            $this->assign('domain',$this->request->domain());
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['sort']=$param['sort'];
            $data['img']=$param['img'];
            $data['url']=$param['url'];
            $data['time']=time();
            $data['uptime']=time();
            $res=Db::name('banner')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function banner_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $data=Db::name('banner')->where('id',$param['id'])->find();
            $this->assign('data',$data);
            $this->assign('domain',$this->request->domain());
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['sort']=$param['sort'];
            $data['img']=$param['img'];
            $data['url']=$param['url'];
            $data['uptime']=time();
            $res= Db::name('banner')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function banner_del(){
        $param = input('post.');
        $res= Db::name('banner')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('banner')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('banner')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
}
